{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sort Even and Odd Indices Independently"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sortEvenOdd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #对奇偶下标分别排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。根据下述规则重排 <code>nums</code> 中的值：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>按 <strong>非递增</strong> 顺序排列 <code>nums</code> <strong>奇数下标</strong> 上的所有值。\n",
    "\n",
    "\t<ul>\n",
    "\t\t<li>举个例子，如果排序前 <code>nums = [4,<em><strong>1</strong></em>,2,<em><strong>3</strong></em>]</code> ，对奇数下标的值排序后变为 <code>[4,<em><strong>3</strong></em>,2,<em><strong>1</strong></em>]</code> 。奇数下标 <code>1</code> 和 <code>3</code> 的值按照非递增顺序重排。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li>按 <strong>非递减</strong> 顺序排列 <code>nums</code> <strong>偶数下标</strong> 上的所有值。\n",
    "\t<ul>\n",
    "\t\t<li>举个例子，如果排序前 <code>nums = [<em><strong>4</strong></em>,1,<em><strong>2</strong></em>,3]</code> ，对偶数下标的值排序后变为 <code>[<em><strong>2</strong></em>,1,<em><strong>4</strong></em>,3]</code> 。偶数下标 <code>0</code> 和 <code>2</code> 的值按照非递减顺序重排。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ol>\n",
    "\n",
    "<p>返回重排 <code>nums</code> 的值之后形成的数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [4,1,2,3]\n",
    "<strong>输出：</strong>[2,3,4,1]\n",
    "<strong>解释：</strong>\n",
    "首先，按非递增顺序重排奇数下标（1 和 3）的值。\n",
    "所以，nums 从 [4,<em><strong>1</strong></em>,2,<em><strong>3</strong></em>] 变为 [4,<em><strong>3</strong></em>,2,<em><strong>1</strong></em>] 。\n",
    "然后，按非递减顺序重排偶数下标（0 和 2）的值。\n",
    "所以，nums 从 [<em><strong>4</strong></em>,1,<em><strong>2</strong></em>,3] 变为 [<em><strong>2</strong></em>,3,<em><strong>4</strong></em>,1] 。\n",
    "因此，重排之后形成的数组是 [2,3,4,1] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [2,1]\n",
    "<strong>输出：</strong>[2,1]\n",
    "<strong>解释：</strong>\n",
    "由于只有一个奇数下标和一个偶数下标，所以不会发生重排。\n",
    "形成的结果数组是 [2,1] ，和初始数组一样。 \n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sort-even-and-odd-indices-independently](https://leetcode.cn/problems/sort-even-and-odd-indices-independently/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sort-even-and-odd-indices-independently](https://leetcode.cn/problems/sort-even-and-odd-indices-independently/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[4,1,2,3]', '[2,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even_list = []\n",
    "        odd_list = []\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2 == 0:\n",
    "                even_list.append(nums[i])\n",
    "            else:\n",
    "                odd_list.append(nums[i])\n",
    "        \n",
    "        # 奇数（降序）、偶数（升序）\n",
    "        odd_list.sort(reverse=True)\n",
    "        even_list.sort()\n",
    "        # print(odd_list)\n",
    "        # print(even_list)\n",
    "\n",
    "        # 整理结果\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2 == 0:\n",
    "                nums[i] = even_list.pop(0)\n",
    "            else:\n",
    "                nums[i] = odd_list.pop(0)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:        \n",
    "        js=[nums[x] for x in range(1,len(nums),2)]   \n",
    "        js.sort()           \n",
    "        od=[nums[x] for x in range(0,len(nums),2)]  \n",
    "        od.sort(reverse=True)              \n",
    "        res=[]\n",
    "        #print(js,od)\n",
    "        while True:\n",
    "            if od:\n",
    "                res.append(od.pop())\n",
    "            if js:\n",
    "                res.append(js.pop())\n",
    "            if not od and not js:\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2],reverse = False)\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: list[int]) -> list[int]:\n",
    "        a=[[],[]]\n",
    "        at=0\n",
    "        for i in nums:\n",
    "            a[at].append(i)\n",
    "            at^=1\n",
    "        a[0].sort(reverse=False)\n",
    "        a[1].sort(reverse=True)\n",
    "        at=0\n",
    "        for i in range(len(nums)):\n",
    "            nums[i]=a[at][i//2]\n",
    "            at^=1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        ji=nums[1::2]\n",
    "        ou=nums[0::2]\n",
    "        ji.sort(reverse=True)\n",
    "        ou.sort()\n",
    "        lst=[]\n",
    "        if len(ji)==len(ou):\n",
    "            for i in range(len(ji)):\n",
    "                lst.extend([ou[i],ji[i]])\n",
    "        elif len(ji)<len(ou):\n",
    "            for i in range(len(ji)):\n",
    "                lst.extend([ou[i],ji[i]])\n",
    "            lst.extend([ou[-1]])\n",
    "        elif len(ji)>len(ou):\n",
    "            for i in range(len(ou)):\n",
    "                lst.extend([ou[i],ji[i]])\n",
    "            lst.extend([ji[-1]])\n",
    "        return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        nums[0::2]=sorted(nums[0::2])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "        return nums\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: list[int]) -> list[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2]=sorted(nums[::2])\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        ji=[]\n",
    "        ou=[]\n",
    "        for i in range(0,len(nums),2):\n",
    "            ou.append(nums[i])\n",
    "        for i in range(1,len(nums),2):\n",
    "            ji.append(nums[i])\n",
    "        ou.sort(reverse=0)\n",
    "        ji.sort(reverse=1)\n",
    "        e=[]\n",
    "        for j in range(len(nums)):\n",
    "            if j%2==0:\n",
    "                e.append(ou[j//2])\n",
    "            else:\n",
    "                e.append(ji[j//2])\n",
    "        return e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2]=sorted(nums[::2])\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        # 列表的原地赋值\n",
    "        nums[0::2] = sorted(nums[0::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        List1 = nums[0::2]\n",
    "        List2 = nums[1::2]\n",
    "        List1 = sorted(List1)\n",
    "        List2 = sorted(List2,reverse=True)\n",
    "        nums[::2]=List1\n",
    "        nums[1::2]=List2\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2]=sorted(nums[::2])\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        return nums\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[1::2] = sorted(nums[1::2],reverse = True)\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        n1 = [nums[i] for i in range(len(nums)) if i % 2 == 0]\n",
    "        n1.sort()\n",
    "        n2 = [nums[i] for i in range(len(nums)) if i % 2 == 1]\n",
    "        n2.sort(reverse=True)\n",
    "        ans = []\n",
    "        for i in range(max(len(n1), len(n2))):\n",
    "            ans.append(n1[i])\n",
    "            if i < len(n2):\n",
    "                ans.append(n2[i])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1=[]\n",
    "        lst2=[]\n",
    "        lst=[]\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            if i%2!=0:\n",
    "                lst2.append(nums[i])\n",
    "            else:\n",
    "                lst1.append(nums[i])\n",
    "        lst1=sorted(lst1)\n",
    "        lst2=sorted(lst2,reverse=True)\n",
    "        num=list(zip(lst1,lst2))\n",
    "        for i in num:\n",
    "            lst.append(i[0])\n",
    "            lst.append(i[1])\n",
    "        if n%2!=0:\n",
    "            b=int((n-1)/2)\n",
    "            lst.append(lst1[b])\n",
    "        return lst\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2], nums[1::2] = sorted(nums[::2]), sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2], reverse = True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2], reverse=True)\n",
    "        \n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        evens = sorted(nums[::2])\n",
    "        odds = sorted(nums[1::2], key=lambda x: -x)\n",
    "        return [evens[i // 2] if i % 2 == 0 else odds[i // 2] for i in range(len(nums))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2],nums[1::2]=sorted(nums[::2]),sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1 = sorted(nums[::2],reverse=False)\n",
    "        lst2 = sorted(nums[1::2],reverse=True)\n",
    "        nums[::2] = lst1\n",
    "        nums[1::2] = lst2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            even = sorted(nums[::2])\n",
    "            odd = sorted(nums[1::2])[::-1]\n",
    "            nums[::2]=even\n",
    "            nums[1::2]=odd\n",
    "            return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        qi=[]\n",
    "        ou=[]\n",
    "        for i in range(len(nums)):\n",
    "            if i%2==0:\n",
    "                ou.append(nums[i])\n",
    "            else:\n",
    "                qi.append(nums[i])\n",
    "        qi.sort(reverse=True)\n",
    "        ou.sort()\n",
    "        nn=min(len(qi),len(ou))\n",
    "        lst=[]\n",
    "        for i in range(nn):\n",
    "            lst.append(ou[i])\n",
    "            lst.append(qi[i])\n",
    "        if len(ou)>nn:\n",
    "            lst.append(ou[nn])\n",
    "        return lst\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: list[int]) -> list[int]:\n",
    "        a=[[],[]]\n",
    "        at=0\n",
    "        for i in nums:\n",
    "            a[at].append(i)\n",
    "            at^=1\n",
    "        a[0].sort(reverse=False)\n",
    "        a[1].sort(reverse=True)\n",
    "        at=0\n",
    "        for i in range(len(nums)):\n",
    "            nums[i]=a[at][i//2]\n",
    "            at^=1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        '''even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums'''\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        for i in range(len(odd)):\n",
    "            even.insert(2*i+1,odd[i])\n",
    "        return even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[1::2] = sorted(nums[1::2], reverse=True)\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1 = []\n",
    "        lst2 = []\n",
    "        for i in range(1,len(nums),2):\n",
    "            lst1.append(nums[i])\n",
    "        for i in range(0,len(nums),2):\n",
    "            lst2.append(nums[i])\n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort(reverse=False)\n",
    "        for i in range(1,len(nums),2):\n",
    "            nums[i] = lst1[int((i-1)/2)]\n",
    "        for i in range(0,len(nums),2):\n",
    "            nums[i] = lst2[int(i/2)]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1 = sorted(nums[::2],reverse=False)\n",
    "        lst2 = sorted(nums[1::2],reverse=True)\n",
    "        nums[::2] = lst1\n",
    "        nums[1::2] = lst2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst_j = []\n",
    "        lst_o = []\n",
    "        num_new = []\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2 == 0:\n",
    "                lst_o.append(nums[i])\n",
    "            else:\n",
    "                lst_j.append(nums[i])\n",
    "        lst_o.sort(reverse=False)\n",
    "        lst_j.sort(reverse=True)\n",
    "        if len(lst_o) == len(lst_j):\n",
    "            for j in range(len(lst_o)):\n",
    "                num_new.append(lst_o[j])\n",
    "                num_new.append(lst_j[j])         \n",
    "        else:\n",
    "            for j in range(len(lst_j)):\n",
    "                num_new.append(lst_o[j])\n",
    "                num_new.append(lst_j[j])\n",
    "            num_new.append(lst_o[-1])    \n",
    "        return num_new    \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2], nums[1::2] = sorted(nums[::2]), sorted(nums[1::2], reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        nums[0::2]=sorted(nums[0::2])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1,lst2=[],[]\n",
    "        a=0\n",
    "        for i in nums:\n",
    "            if a%2!=0:\n",
    "                lst1.append(i)\n",
    "                a+=1\n",
    "            else:\n",
    "                lst2.append(i)\n",
    "                a+=1\n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort()\n",
    "        for i in range(len(lst1)):\n",
    "            lst2.insert(2*i+1,lst1[i])\n",
    "        return lst2\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        def odd(num):\n",
    "            if num % 2 == 1:\n",
    "                return True\n",
    "            return False\n",
    "        n = len (nums)\n",
    "        n1 = []\n",
    "        n2 = []\n",
    "        for i in range(n):\n",
    "            if odd(i):\n",
    "                n1.append(nums[i])\n",
    "            else:\n",
    "                n2.append(nums[i])\n",
    "        n1.sort(reverse=True)\n",
    "        n2.sort()\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if odd(i):\n",
    "                res.append(n1.pop(0))\n",
    "            else:\n",
    "                res.append(n2.pop(0))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse = True)\n",
    "        return nums\n",
    "\n",
    "      \n",
    "\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        lst1=[]\n",
    "        lst2=[]\n",
    "        for i in range(n):\n",
    "            if i%2==0:\n",
    "                lst2.append(nums[i])\n",
    "            else:\n",
    "                lst1.append(nums[i])    \n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort(reverse=False)\n",
    "        for i in range(n):\n",
    "            if i%2==0:\n",
    "                nums[i]=lst2[i//2]\n",
    "            else:\n",
    "                nums[i]=lst1[(i-1)//2]\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2]=sorted(nums[::2])\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        return nums\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        list1 = []\n",
    "        list2 = []\n",
    "        new_list = [0]*len(nums)\n",
    "        for i in range (len(nums)):\n",
    "            if i%2 == 0 :\n",
    "                list2.append(nums[i])\n",
    "            else:\n",
    "                list1.append(nums[i])\n",
    "        for j in range (len(nums)):\n",
    "            if j%2 == 0 :\n",
    "                n = list2.index(min(list2))\n",
    "                new_list[j] = list2.pop(n)\n",
    "            else:\n",
    "                n = list1.index(max(list1))\n",
    "                new_list[j] = list1.pop(n)\n",
    "        return new_list\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "\n",
    "        nums[1::2] = sorted(nums[1::2], reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1,lst2=[],[]\n",
    "        a=0\n",
    "        for i in nums:\n",
    "            if a%2!=0:\n",
    "                lst1.append(i)\n",
    "                a+=1\n",
    "            else:\n",
    "                lst2.append(i)\n",
    "                a+=1\n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort()\n",
    "        for i in range(len(lst1)):\n",
    "            lst2.insert(2*i+1,lst1[i])\n",
    "        return lst2\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        ans1 = []\n",
    "        ans2 = []\n",
    "        ans = []\n",
    "        for i, x in enumerate(nums):\n",
    "            if i % 2 == 0:\n",
    "                ans1.append(x)\n",
    "            else:\n",
    "                ans2.append(x)\n",
    "        ans1.sort()\n",
    "        ans2 = sorted(ans2)[::-1]\n",
    "        for x, y in zip(ans1, ans2):\n",
    "            ans.append(x)\n",
    "            ans.append(y)\n",
    "        if len(ans1) > len(ans2):\n",
    "            ans.append(ans1[-1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        evens = sorted(nums[::2])\n",
    "        odds = sorted(nums[1::2], key=lambda x: -x)\n",
    "        nums[::2] = evens\n",
    "        nums[1::2] = odds\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        new_lst=[]\n",
    "        lst1=nums[1::2]\n",
    "        lst2=nums[::2]\n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort()\n",
    "        for pair in zip(lst2,lst1):\n",
    "            new_lst.extend(pair)\n",
    "        new_lst.extend(lst2[len(lst1):])\n",
    "        return new_lst\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        new_lst=[]\n",
    "        lst1=nums[1::2]\n",
    "        lst2=nums[::2]\n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort()\n",
    "        for pair in zip(lst2,lst1):\n",
    "            new_lst.extend(pair)\n",
    "        new_lst.extend(lst2[len(lst1):])\n",
    "        return new_lst\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])\n",
    "        odd = odd[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums\n",
    "        # lst1 = []\n",
    "        # lst2 = []\n",
    "        # lst3 = []\n",
    "        # for i in range(0,len(nums),2):\n",
    "        #     lst1.append(nums[i])\n",
    "        #     if i+1 in range(len(nums)):\n",
    "        #         lst2.append(nums[i+1])\n",
    "        # even = sorted(lst1)   \n",
    "        # odd = sorted(lst2).reverse()    \n",
    "        # for j in range(len(even)):\n",
    "        #     lst3.append(even[j])\n",
    "        #     if j+1 in range(len(nums)):\n",
    "        #         lst3.append(odd[j])\n",
    "        # return lst3\n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        lst1=nums[1::2]\n",
    "        lst_1=sorted(lst1,reverse=True)\n",
    "        lst2=nums[0::2]\n",
    "        lst_2=sorted(lst2)\n",
    "        nums[::2],nums[1::2]=lst_2,lst_1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        t1 = []\n",
    "        t2 = []\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2 == 0:\n",
    "                t1.append(nums[i])\n",
    "            else:\n",
    "                t2.append(nums[i])\n",
    "        t2.sort(reverse = True)\n",
    "        t1.sort()\n",
    "        j = 0\n",
    "        k = 0\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2 == 0:\n",
    "                nums[i] = t1[j]\n",
    "                j += 1\n",
    "            else:\n",
    "                nums[i] = t2[k]\n",
    "                k += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        if n % 2 == 0:\n",
    "            for i in range(n // 2):\n",
    "                for j in range(0,n - 2,2):\n",
    "                    if nums[j] > nums[j + 2]:\n",
    "                        nums[j] , nums[j + 2] = nums[j + 2] , nums[j]\n",
    "            for i in range(n // 2):            \n",
    "                for j in range(1,n - 1,2):\n",
    "                    if nums[j] < nums[j + 2]:\n",
    "                        nums[j] , nums[j + 2] = nums[j + 2] , nums[j]\n",
    "        else:\n",
    "            for i in range((n+1) // 2): \n",
    "                for j in range(0, n - 1, 2):\n",
    "                    if nums[j] > nums[j + 2]:\n",
    "                        nums[j], nums[j + 2] = nums[j + 2], nums[j]\n",
    "            for i in range((n-1) // 2): \n",
    "                for j in range(1, n - 2, 2):\n",
    "                    if nums[j] < nums[j + 2]:\n",
    "                        nums[j], nums[j + 2] = nums[j + 2], nums[j]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2]=sorted(nums[::2])\n",
    "        nums[1::2]=sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse = True)\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = []\n",
    "        odd = []\n",
    "        for i, n in enumerate(nums):\n",
    "            if i % 2 :\n",
    "                odd.append(n)\n",
    "            else:\n",
    "                even.append(n)\n",
    "        even.sort()\n",
    "        odd.sort(reverse=True)\n",
    "        ret = []\n",
    "        for i, n in enumerate(even):\n",
    "            ret.append(n)\n",
    "            if i < len(odd):\n",
    "                ret.append(odd[i])\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        even = [nums[i] for i in range(0, n, 2)]\n",
    "        odd = [nums[i] for i in range(1, n, 2)]\n",
    "        even.sort()\n",
    "        odd.sort(reverse=True)\n",
    "        print(even, odd)\n",
    "        ans = [0] * n\n",
    "        for i in range(len(even)):\n",
    "            ans[i << 1] = even[i]\n",
    "        for i in range(len(odd)):\n",
    "            ans[(i << 1) + 1] = odd[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        l=len(nums)\n",
    "        lst=nums\n",
    "        lst1=sorted(nums[::2])\n",
    "        lst2=sorted(nums[1::2],reverse=True)\n",
    "        n=0\n",
    "        m=0\n",
    "        for i in range(l) :\n",
    "            if i%2==0:\n",
    "               lst[i]=lst1[n]\n",
    "               n+=1\n",
    "            else:\n",
    "               lst[i]=lst2[m]\n",
    "               m+=1\n",
    "        return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        a1,a2=[],[]\n",
    "        for i in range(0,n,2):\n",
    "            a1.append(nums[i])\n",
    "            if i+1<n:\n",
    "                a2.append(nums[i+1])\n",
    "        a1=sorted(a1,reverse=True)\n",
    "        a2=sorted(a2)\n",
    "        res=[]\n",
    "        while a1 or a2:\n",
    "            if a1:\n",
    "                res.append(a1.pop())\n",
    "            if a2:\n",
    "                res.append(a2.pop())\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "        nums[::2]=sorted(nums[::2])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        new_lst=[]\n",
    "        lst1=nums[1::2]\n",
    "        lst2=nums[::2]\n",
    "        lst1.sort(reverse=True)\n",
    "        lst2.sort()\n",
    "        for pair in zip(lst2,lst1):\n",
    "            new_lst.extend(pair)\n",
    "        new_lst.extend(lst2[len(lst1):])\n",
    "        return new_lst\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse = True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even =sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2]=even\n",
    "        nums[1::2]=odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "\n",
    "\n",
    "\n",
    "        tmpeven = nums[::2]\n",
    "        tmpodd = nums[1::2] \n",
    "        tmpeven.sort()\n",
    "        tmpodd.sort(reverse=True)\n",
    "\n",
    "\n",
    "        nums[::2] = tmpeven\n",
    "\n",
    "        nums[1::2] = tmpodd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        oddarr = []\n",
    "        evenarr = []\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2:\n",
    "                oddarr.append(nums[i])\n",
    "            else:\n",
    "                evenarr.append(nums[i])\n",
    "            \n",
    "        oddarr.sort(reverse=True)\n",
    "        evenarr.sort()\n",
    "        ans = []\n",
    "\n",
    "        while oddarr or evenarr:\n",
    "            if evenarr:\n",
    "                ans.append(evenarr.pop(0))\n",
    "            if oddarr:\n",
    "                ans.append(oddarr.pop(0))\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2],reverse=True)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        nums[::2] = sorted(nums[::2])\n",
    "        nums[1::2] = sorted(nums[1::2])[::-1]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        even = sorted(nums[::2])\n",
    "        odd = sorted(nums[1::2])[::-1]\n",
    "        nums[::2] = even\n",
    "        nums[1::2] = odd\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortEvenOdd(self, nums: List[int]) -> List[int]:\n",
    "        x = list(sorted(nums[::2]))\n",
    "        y = list(sorted(nums[1::2], reverse=True))\n",
    "        \n",
    "        n = len(nums)\n",
    "        for i in range(n // 2):\n",
    "            nums[2 * i] = x[i]\n",
    "            nums[2 * i + 1] = y[i]\n",
    "\n",
    "        if n % 2 == 1:\n",
    "            nums[-1] = x[-1]\n",
    "\n",
    "        # print(x, y)\n",
    "        return nums"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
